#include "calc_neighborhoods.h"
#include <string.h>

int main(int argc, char** argv) {
  struct store * atomStore, * neighStore;
  struct mTree * atomTree,  * neighTree;
  
  char * neigh_base = argv[1];
  char * neigh_fat, * neigh_tree, * neigh_tree_fat;

  int i;

  neigh_fat = malloc(sizeof(char) * (strlen(neigh_base) + 4 + 1));
  neigh_tree = malloc(sizeof(char) * (strlen(neigh_base) + 6 + 1));
  neigh_tree_fat = malloc(sizeof(char) * (strlen(neigh_base) + 10 + 1));

  strcpy(neigh_fat,      neigh_base);
  strcpy(neigh_tree,     neigh_base);
  strcpy(neigh_tree_fat, neigh_base);

  strcat(neigh_fat,      ".fat");
  strcat(neigh_tree,     ".mTree");
  strcat(neigh_tree_fat, ".mTree.fat");

  printf("Using these files for tree: %s, %s, %s, %s\n", neigh_base, neigh_fat, neigh_tree, neigh_tree_fat);

  neighStore = store_open_cached(neigh_base, neigh_fat, sizeof(struct aa_bond_neighborhood), 100, 5000000);
  neighTree  = mTree_open_cached(neighStore, neigh_tree, neigh_tree_fat, 200000, (double(*)(const void*,const void*))&aa_bond_neighborhood_distance);

  for(i = 2; i < argc; i++) {

    char * atom_base = argv[i];
    char * atom_fat, * atom_tree, * atom_tree_fat;

    printf("%s\n", atom_base);

    atom_fat = malloc(sizeof(char) * (strlen(atom_base) + 4 + 1));
    atom_tree = malloc(sizeof(char) * (strlen(atom_base) + 6 + 1));
    atom_tree_fat = malloc(sizeof(char) * (strlen(atom_base) + 10 + 1));
    
    strcpy(atom_fat,      atom_base);
    strcpy(atom_tree,     atom_base);
    strcpy(atom_tree_fat, atom_base);
    
    strcat(atom_fat,      ".fat");
    strcat(atom_tree,     ".mTree");
    strcat(atom_tree_fat, ".mTree.fat");
    
    atomStore  = store_open_cached(atom_base, atom_fat, sizeof(struct atom), 100, 120000);
    atomTree   = mTree_open_cached(atomStore, atom_tree, atom_tree_fat, 80000, (double(*)(const void*,const void*))&atom_distance);
    
    calc_neighborhoods(argv[1], 30 * 1000, atomStore, atomTree, neighStore, neighTree);

    mTree_close(atomTree);
    store_close(atomStore);

    if(i % 10 == 0) {
      printf("Neighborhood tree stats:\n");
      mTree_printStats(neighTree);

    }
    
    free(atom_fat);
    free(atom_tree);
    free(atom_tree_fat);


  }
   
  
  mTree_close(neighTree);
  store_close(neighStore);
  
  free(neigh_fat);
  free(neigh_tree);
  free(neigh_tree_fat);
  
  return 0;

}
